package com.appstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.appstore.dao.IAdministratorrDao;
import com.appstore.exception.DaoException;
import com.appstore.util.ConnectionManager;

/**
 * The implementation of IAdministratorDao
 * 
 * @author zhbhun
 * 
 */
public class AdministratorDaoimpl implements IAdministratorrDao {

	private static AdministratorDaoimpl administratorDaoimpl;
	private ConnectionManager connectionManager;

	private AdministratorDaoimpl() {
		connectionManager = ConnectionManager.getConnectionManager();
	}

	/**
	 * instance AdministratorDaoimpl
	 * 
	 * @return
	 */
	public static AdministratorDaoimpl createInstatce() {
		if (administratorDaoimpl == null) {
			administratorDaoimpl = new AdministratorDaoimpl();
		}
		return administratorDaoimpl;
	}

	/**
	 * get the instance of AdministratorDaoimpl
	 * 
	 * @return
	 */
	public static AdministratorDaoimpl getAdministratorDaoimpl() {
		return administratorDaoimpl;
	}

	@Override
	public int findAdministrator(String username, String password)
	        throws DaoException {
		int id = -1;
		Connection connection = null;
		// get connection
		try {
			connection = connectionManager.getConnetion();
		} catch (SQLException e) {
			throw new DaoException("can not get connection", e);
		}
		// find data
		try {
			String sql = "select id from administrator where username = ? and password = ?";
			connection.setAutoCommit(false);
			PreparedStatement pStatement = connection.prepareStatement(sql);
			pStatement.setString(1, username);
			pStatement.setString(2, password);
			ResultSet resultSet = pStatement.executeQuery();
			while (resultSet.next()) {
				id = resultSet.getInt("id");
			}
			resultSet.close();
			pStatement.close();
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				throw new DaoException("there is error while rollback", e);
			}
			throw new DaoException("there is error while executing sql", e);
		}
		// close connection
		try {
			connection.close();
		} catch (SQLException e) {
			throw new DaoException("can not close connection", e);
		}
		return id;
	}

	@Override
	public void changePassword(int id, String newPwd) throws DaoException {
		Connection connection = null;
		// get connection
		try {
			connection = connectionManager.getConnetion();
		} catch (SQLException e) {
			throw new DaoException("can not get connection", e);
		}
		// find data
		try {
			String sql = "update administrator set password = '" + newPwd
			        + "' where id = ?";
			connection.setAutoCommit(false);
			PreparedStatement pStatement = connection.prepareStatement(sql);
			pStatement.setInt(1, id);
			pStatement.executeUpdate();
			pStatement.close();
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				throw new DaoException("there is error while rollback", e);
			}
			throw new DaoException("there is error while executing sql", e);
		}
		// close connection
		try {
			connection.close();
		} catch (SQLException e) {
			throw new DaoException("can not close connection", e);
		}
	}

}
